package game.module.turntables.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import db.proto.PlayTurntable;
import db.proto.PlayTurntable.ATurntables;
import game.common.DaoCommonHandler;
import game.db.DataSourceManager;
import game.module.hall.bean.SettingBean;
import game.module.turntables.bean.TurntableBean;
import game.module.user.bean.PlayerBean;
import lion.common.MyBlob;

public class TurntableDBDao {
	private static Logger logger = LoggerFactory.getLogger(TurntableDBDao.class);

	private DataSource dataSource = DataSourceManager.getInstance().getDataSource();

	static class SingletonHolder {
		static TurntableDBDao instance = new TurntableDBDao();
	}

	public static TurntableDBDao getInstance() {
		return SingletonHolder.instance;
	}

	private ResultSetHandler<TurntableBean> aturntableHandler = new ResultSetHandler<TurntableBean>() {

		@Override
		public TurntableBean handle(ResultSet rs) throws SQLException {
			TurntableBean turntableBean = null;
			if (rs.next()) {
				turntableBean = new TurntableBean();
				turntableBean.setId(rs.getInt("id"));
				turntableBean.setPlayerid(rs.getInt("playerid"));
				try {
					InputStream is = rs.getBinaryStream("turntables");
					if(is!=null) {
						ATurntables turntable = PlayTurntable.ATurntables.parseFrom(is);
						turntableBean.setTurntableList(turntable);
					}
				}catch (IOException e) {
					e.printStackTrace();
				}
				
			}
			return turntableBean;
		}
		
	};
	public TurntableBean getTurntableById(int playerId) {
		TurntableBean ret = null;
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			ret = runner.query("SELECT * from truntables where playerid = ?", aturntableHandler, playerId);
		} catch (SQLException e) {
		logger.error("", e);
			return ret;
		}
		return ret;
	}
	
	public void updateTurntable(TurntableBean turntableBean) {
		ATurntables Turntable = null;
		if(turntableBean!=null)
			Turntable = turntableBean.getTurntableList();
		QueryRunner runner = new QueryRunner(dataSource);
		byte[] content1 = new byte[0];
		if(Turntable!=null) {
			content1 = Turntable.toByteArray();
		}
		try {
			MyBlob blob = new MyBlob(content1);
			runner.update("update truntables set turntables=? where id =?",
					blob, turntableBean.getId());
		} catch (SQLException e) {
			logger.error("", e);
		}
	}
	public boolean addTurntable(TurntableBean TurntableBean) {
		boolean addRet = false;
		ATurntables Turntable = TurntableBean.getTurntableList();
		byte[] content1 = new byte[0];
		if(Turntable!=null) {
			content1 = Turntable.toByteArray();
		}
		QueryRunner runner = new QueryRunner(dataSource);
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			MyBlob blob = new MyBlob(content1);
			int ret = runner.update(conn,
					"insert into truntables(playerid,turntables)"
							+ "values(?,?)",
					TurntableBean.getPlayerid(),blob);
			if (ret > 0) {
				addRet = true;
			}
			if (addRet) {
				int theId = runner.query(conn, "SELECT LAST_INSERT_ID();", DaoCommonHandler.Integerhandler);
				TurntableBean.setId(theId);
			}
		} catch (SQLException e) {
			logger.error("", e);
		} finally {
			DbUtils.closeQuietly(conn);
		}
		return addRet;
	}
}
//	private ResultSetHandler<TurntableBean> aSettingHandler = new ResultSetHandler<TurntableBean>() {
//		@Override
//		public TurntableBean handle(ResultSet rs) throws SQLException {
//			TurntableBean turntableBean = null;
//			if (rs.next()) {
//				turntableBean = new TurntableBean();
//				turntableBean.setId(rs.getInt("id"));
//				turntableBean.setPlayerid(rs.getInt("playerid"));
//				try {
//					InputStream is = rs.getBinaryStream("turntables");
//					if(is!=null) {
//						ProtoMessageTurntable turntable = ProtoMessageTurntable.parseFrom(is);
//						turntableBean.setTurntableList(turntable);
//					}
//				}catch (IOException e) {
//					e.printStackTrace();
//				}
////				turntableBean.setTurntable_id(rs.getInt("turntable_id"));
////				turntableBean.setGain_retain_num(rs.getInt("gain_retain_num"));
////				turntableBean.setType(rs.getInt("type"));
////				turntableBean.setSure_gain_remain_num(rs.getInt("sure_gain_remain_num"));
//			}
//			return turntableBean;
//		};
//		}

//	private ResultSetHandler<List<TurntableBean>> multiHandler = new ResultSetHandler<List<TurntableBean>>() {
//
//		@Override
//		public List<TurntableBean> handle(ResultSet rs) throws SQLException {
//			List<TurntableBean> userBeanList = new ArrayList<TurntableBean>();
//			while (rs.next()) {
//				TurntableBean turntable = new TurntableBean();
//				turntable = new TurntableBean();
//				turntable.setId(rs.getInt("id"));
//				turntable.setPlayerid(rs.getInt("playerid"));
//				turntable.setTurntable_id(rs.getInt("turntable_id"));
//				turntable.setGain_retain_num(rs.getInt("gain_retain_num"));
//				turntable.setType(rs.getInt("type"));
//				turntable.setSure_gain_remain_num(rs.getInt("sure_gain_remain_num"));
//				userBeanList.add(turntable);
//			}
//			return userBeanList;
//		}
//
//	};

//	public List<TurntableBean> getTurntableById(int playerId) {
//		List<TurntableBean> ret = null;
//		QueryRunner runner = new QueryRunner(dataSource);
//		try {
//			ret = runner.query("SELECT * from truntables where playerid = ?", multiHandler, playerId);
//		} catch (SQLException e) {
//			logger.error("", e);
//			return ret;
//		}
//		return ret;
//	}
//
//	public void updateTurntable(TurntableBean turntableBean) {
//		QueryRunner runner = new QueryRunner(dataSource);
//
//		try {
//			runner.update("update truntables set turntable_id=?,gain_retain_num=?,sure_gain_remain_num=? where id =?",
//					turntableBean.getTurntable_id(), turntableBean.getGain_retain_num(),
//					turntableBean.getSure_gain_remain_num(), turntableBean.getId());
//		} catch (SQLException e) {
//			logger.error("", e);
//		}
//	}
//
//	public boolean addTurntable(TurntableBean TurntableBean) {
//		boolean addRet = false;
//		QueryRunner runner = new QueryRunner(dataSource);
//		Connection conn = null;
//		try {
//			conn = dataSource.getConnection();
//			int ret = runner.update(conn,
//					"insert into truntables(playerid,turntable_id,gain_retain_num,type,sure_gain_remain_num)"
//							+ "values(?,?,?,?,?)",
//					TurntableBean.getPlayerid(), TurntableBean.getTurntable_id(), TurntableBean.getGain_retain_num(),
//					TurntableBean.getType(), TurntableBean.getSure_gain_remain_num());
//			if (ret > 0) {
//				addRet = true;
//			}
//			if (addRet) {
//				int theId = runner.query(conn, "SELECT LAST_INSERT_ID();", DaoCommonHandler.Integerhandler);
//				TurntableBean.setId(theId);
//			}
//		} catch (SQLException e) {
//			logger.error("", e);
//		} finally {
//			DbUtils.closeQuietly(conn);
//		}
//		return addRet;
//	}

//}
